import { useEffect, useState } from "react";
import { IfetchProps } from "../type/table";
import { BaseTableParams } from "../type";
import { BaseTableColumns } from "../type/active";

export const useFetchTableList = <T>(props: IfetchProps<T>) => {
  const [dataSource, setDataSource] = useState<BaseTableColumns []>([]);
  const [total, setTotal] = useState(0);
  const [filterParams, setFilterParams] = useState(new BaseTableParams());

  useEffect(() => {
    getData();
  }, [filterParams]);
  const getData = async () => {
    const { dataSource, total }: any = await props.API(filterParams);
    setDataSource(dataSource)
    setTotal(total)

  };
  return {
    dataSource,
    total,
    filterParams,
    setFilterParams
  };
};
